#!/bin/bash -e
#
# Build ARM kernel 4.4.y for QEMU Raspberry Pi Emulation
#
#######################################################

TOOLCHAIN=arm-linux-gnueabihf
COMMIT=38f2800e6e94019a00a2880bfed8fba37aea2e5d
COMMIT=raspberrypi-kernel_1.20161100-1
COMMIT=""
INSTALL_PACKAGES=""
USE_GIT=1
USB_WEBCAM_MODULES=""    # add USB & V4L modules for USB webcam support (didn't work as static)

if [ -f build-kernel-qemu.conf ] ; then
	. build-kernel-qemu.conf
fi

if [ "$INSTALL_PACKAGES" ] ; then
	sudo apt-get update
	sudo apt-get install git libncurses5-dev gcc-arm-linux-gnueabihf
fi

if [ "$USE_GIT" ] ; then
	# checking out 4.4.y branch - change it if you want to change kernel version
	# for kernel specific hash, have a looks at : https://github.com/raspberrypi/linux/commits/rpi-4.4.y
	if [ ! -d linux ] ; then
		git clone https://github.com/raspberrypi/linux.git
	else
		echo "Using existing Linux sources!"
	fi
	cd linux
	if [ "$COMMIT" ] ; then
		git checkout "$COMMIT"
	fi
else
	if [ -z "$COMMIT" ] ; then echo "COMMIT missing!" >&2 ; exit 1 ; fi
	wget -c https://github.com/raspberrypi/linux/archive/${COMMIT}.zip -O linux-${COMMIT}.zip
	rm -rf linux-${COMMIT}
	unzip linux-${COMMIT}.zip
	cd linux-${COMMIT}
fi

patch -p1 < ../linux-arm.patch

make ARCH=arm versatile_defconfig
KERNEL_VERSION=$(make kernelversion)
KERNEL_TARGET_FILE_NAME=../qemu-kernel-$KERNEL_VERSION
MODULES_INSTALL_PATH=../qemu-kernel-$KERNEL_VERSION-modules
echo "Building Qemu Raspberry Pi kernel qemu-kernel-$KERNEL_VERSION"

cat >> .config << EOF
CONFIG_CROSS_COMPILE="$TOOLCHAIN"
CONFIG_CPU_V6=y
CONFIG_ARM_ERRATA_411920=y
CONFIG_ARM_ERRATA_364296=y
CONFIG_AEABI=y
CONFIG_OABI_COMPAT=y
CONFIG_PCI=y
CONFIG_SCSI=y
CONFIG_SCSI_SYM53C8XX_2=y
CONFIG_BLK_DEV_SD=y
CONFIG_BLK_DEV_SR=y
CONFIG_DEVTMPFS=y
CONFIG_DEVTMPFS_MOUNT=y
CONFIG_TMPFS=y
CONFIG_INPUT_EVDEV=y
CONFIG_EXT3_FS=y
CONFIG_EXT4_FS=y
CONFIG_VFAT_FS=y
CONFIG_NLS_CODEPAGE_437=y
CONFIG_NLS_ISO8859_1=y
CONFIG_FONT_8x16=y
CONFIG_LOGO=y
CONFIG_VFP=y
CONFIG_CGROUPS=y

CONFIG_MMC_BCM2835=y
CONFIG_MMC_BCM2835_DMA=y
CONFIG_DMADEVICES=y
CONFIG_DMA_BCM2708=y

CONFIG_FHANDLE=y

CONFIG_OVERLAY_FS=y

CONFIG_EXT4_FS_POSIX_ACL=y
CONFIG_EXT4_FS_SECURITY=y
CONFIG_FS_POSIX_ACL=y

CONFIG_IKCONFIG=y
CONFIG_IKCONFIG_PROC=y
EOF

if [ $USB_WEBCAM_MODULES ] ; then
echo "Make sure you have drivers for your webcam selected in menuconfig"
echo >> .config << EOF
# won't work as statically linked
CONFIG_MEDIA_SUPPORT=m
CONFIG_VIDEO_DEV=m
CONFIG_VIDEO_V4L2_COMMON=m
CONFIG_VIDEO_MEDIA=m
CONFIG_VIDEO_V4L2=m
CONFIG_VIDEO_CAPTURE_DRIVERS=m
CONFIG_V4L_USB_DRIVERS=m
CONFIG_USB_VIDEO_CLASS=m
CONFIG_VIDEO_CORE=m
CONFIG_VIDEO_VIDEOBUF2=m
CONFIG_INPUT_CORE=m
CONFIG_VIDEO_UVC=m

CONFIG_USB=m
CONFIG_USB_STORAGE=m
CONFIG_USB_EHCI_HCD=m
CONFIG_USB_OHCI_HCD=m
CONFIG_USB_UHCI_HCD=m
CONFIG_USB_VHCI_HCD=m
CONFIG_USB_GSPCA=m
CONFIG_VIDEO_GSPCA=m

CONFIG_I2C=m

# won't be accepted as modules so linking statically
CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV=y
CONFIG_MEDIA_CAMERA_SUPPORT=y
EOF
fi

cat ../config_ip_tables >> .config

make -j 4 -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- menuconfig
make -j 4 -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- bzImage
cp arch/arm/boot/zImage $KERNEL_TARGET_FILE_NAME

if [ $USB_WEBCAM_MODULES ] ; then
    mkdir -p $MODULES_INSTALL_PATH
    if [ ! -d $MODULES_INSTALL_PATH ] ; then
        echo "Couldn't create webcam modules install directory $MODULES_INSTALL_PATH"
    fi
    make -j 4 -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- modules
    make -j 4 -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- \
        INSTALL_MOD_PATH=$MODULES_INSTALL_PATH modules_install
    echo "Copy modules to Raspberry to /lib/modules/$KERNEL_VERSION"
fi

cd ..
